//全局商品数量
var scopeNum = 1;
//全局规格数据map(json)
var specMap = {};   //相当于new 了一个HashMa

var itemId ;
//商品数量加减
function increment(num){

	if(scopeNum  == 1 && num < 0){
		scopeNum  =1;
	}else{
		scopeNum +=num;
	}
	
	$("#num").val(scopeNum);
}

//选择规格选项,并将选择的数据(规格和规格选项)放到容器中

function checkSpec(spec,specOption){
	specMap[spec] = specOption;
	isSelected();
	for(var i=0;i<skulist.length;i++){
		if(matchMap(skulist[i].spec,specMap)){
			$("#sku-name").html("<h4>"+skulist[i].title+"</h4>");
			$("#news").text(skulist[i].sellPoint);
			$("#price").text(skulist[i].price);
			itemId = skulist[i].id;
			break;
		}
	}
}

function isSelected(){
	var a = $("[name='specOptions']");
	$.each(a,function(i,e){
		var valueList = Object.values(specMap);
		if(valueList.indexOf($(e).text().trim()) >= 0){
			$(e).attr("class","selected");
		}else{
			$(e).attr("class","");
		}
	})
}

function matchMap(map1,map2){
	for(var key in map1){
		if(map1[key] != map2[key]){
			return false;
		}
	}
	
	for(var key in map2){
		if(map1[key] != map2[key]){
			return false;
		}
	}
	return true;
}

function addCart(){
	//被选中的id
	//itemId  scopeNum
	location.href="http://localhost:4011/CartController/saveCart?itemId="+itemId+"&Num="+scopeNum
}